<?php
namespace backend\controllers;

use bc\controller\BackendController;
use backend\service\system\MenuService;


class SysController extends BackendController
{
	protected $allActions;
	
	protected $allActionData;
	
	public function actionIndex()
	{
		
		$this->redirect('/sys/menu-list');
	}
	
	/**
	 * 获取所有菜单
	 */
	public function actionMenuList()
	{
		$params = null;
		
		$search_select = $this->get('search_select');
		if($search_select > 1)
		{
			$params['is_menu'] = $search_select - 1;
		}
		
		$serviceMenu = new MenuService();
		$serviceMenu->sort = 'id ASC';
		$list = $serviceMenu->getAllList($params);
		
		
		$view['list'] = $list;
		$view['search_select'] = $search_select;
		return $this->render('menu-list', $view);
	}
	
	public function actionMenuAdd()
	{
		
		$serviceMenu = new MenuService();
		
		$id = $this->get('id');
		
		//扫描所有action的method
		$this->scanAction();
		
		$actions = $this->unuseAction();
		
		$topMenuList = $serviceMenu->getList(['parent_id' => 0]);
		
		$view['top_menu_list'] = $topMenuList;
		
		$view['actions'] = $actions;
		if($id)
		{
			$row = $serviceMenu->getRow(['id' => $id]);
			$view['row'] = $row;
		}
		
		return $this->render('menu-add', $view);
	}
	
	
	/**
	 * 
	 */
	public function actionMenuSave()
	{
		$serviceMenu = new MenuService();
		$id = $this->get('id', 0);
		
		$name = $this->post('name');
		$url = $this->post('url');
		$module_name = $this->post('module_name');
		$is_menu = $this->post('is_menu');
		$status = $this->post('status');
		$parent_id = $this->post('parent_id');
		
		$arr_module_name = explode('|', $module_name);
		
		
		$data['name'] = $name;
		$data['url'] = $url;
		$data['controller'] = $arr_module_name[0];
		$data['action'] = $arr_module_name[1];
		$data['parent_id'] = $parent_id;
		$data['status'] = $status;
		
		
		if($parent_id == 0)
		{
			$data['level'] = 1;
		}
		else 
		{
			$data['level'] = 2;
		}
		
		if($id)
		{
			$serviceMenu->update($data, $id);
		}
		else 
		{
			$serviceMenu->add($data);
		}
		
		
		$output ['msg'] = '保存成功!';
		$output ['url'] = '/sys/menu-list';
		$this->forward ( $output );
		
	}
	
	
	public function scanAction()
	{
		$namespcace = __NAMESPACE__;
	
		$arrAction = [];
	
		$dirname = dirname ( __FILE__ . '../' );
		$dirs = scandir ( $dirname );
		foreach ( $dirs as $dir )
		{
			if (strpos ( $dir, '.php' ) > 0)
			{
				$className = $namespcace . '\\' . $dir;
				$className = str_replace ( '.php', '', $className );
				$this->getAction ( $className );
			}
		}
	}
	
	/**
	 *
	 * @param Object $class
	 * @return void
	 */
	public function getAction($class)
	{
		$class = new \ReflectionClass ( $class );
		$methods = $class->getMethods ( \ReflectionMethod::IS_PUBLIC );
	
		foreach ( $methods as $method )
		{
	
			if (preg_match ( '/^action[A-Z][A-Za-z0-9-]+$/', $method->name ))
			{
				$actionInfo['controller'] = $method->class;
				$actionInfo['action'] = $method->name;
	
				$this->allActions [] = $actionInfo;
	
			}
		}
	}
	
	private function unuseAction()
	{
		$serviceMenu = new MenuService();
		$serviceMenu->sort = 'id ASC';
		$list = $serviceMenu->getList();
		foreach ($list as $row)
		{
			$tmp['controller'] = $row['controller'];
			$tmp['action'] = $row['action'];
			$this->allActionData[]  = $tmp;
		}
		
		
		foreach ($this->allActions as $key=>$all)
		{
			foreach ($this->allActionData as $action)
			{
				if($all['controller'] == $action['controller'] && $all['action'] == $action['action'])
				{
					unset($this->allActions[$key]);
				}
			}
		}
		
		
		$unsetAction = $this->allActions;
		sort($unsetAction);		
		
		return $unsetAction;
	}
}

